package gsar.client;

import java.io.Serializable;

/*
Copyright (c) 2009, Eric Eaton, University of Maryland Baltimore County
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the University of Maryland Baltimore County nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/** Represents a coordinate in the 2D gridworld.
 * @author Eric Eaton
 */
public class GWCoordinate implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	int x;
	int y;

	public GWCoordinate(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public int getX() { return x; }
	public int getY() { return y; }
	
	public boolean equals(Object c) {
		if (!(c instanceof GWCoordinate))
			return false;
		return equals((GWCoordinate) c);
	}
	
	public boolean equals(GWCoordinate c) {
		return (c.x == x && c.y == y);
	}
	
	public static boolean equals(GWCoordinate c1, GWCoordinate c2) {
		return (c1.x == c2.x && c1.y == c2.y);
	}
	
	@Override
	public int hashCode() {
		return toString().hashCode();
	}
	
	/** Determines whether two coordinates are adjacent, that is they
	 * differ only along one direction by one unit.
	 * @param c1
	 * @param c2
	 * @return whether the two coordinates are adjacent.
	 */
	public static boolean isAdjacent(GWCoordinate c1, GWCoordinate c2) {
		return (Math.abs(c1.getX() - c2.getX()) +
				Math.abs(c1.getY() - c2.getY()) == 1);
	}
	
	
	/** Creates a coordinate from a string representation of the coordinate in the format
	 *  of either "x,y" or "(x,y)"
	 * @param s the string representation
	 * @return the coordinate in the given string
	 */
	public static GWCoordinate fromString(String s) {
		try {
			String[] tokens = s.split(",");
			int x = Integer.parseInt(tokens[0].replaceFirst("\\(","").trim());
			int y = Integer.parseInt(tokens[1].replaceFirst("\\)","").trim());
			return new GWCoordinate(x,y);
		} catch (Exception e) {
			throw new IllegalArgumentException("Improper coordinate format.  Format must be \"x,y\" or \"(x,y)\".");
		}
	}
	
	/** Finds the direction between two adjacent coordinates
	 * @param startCoord the start coordinate
	 * @param endCoord the target coordinate
	 * @return the direction from startCoord to endCoord, or null if the coordinates are not adjacent
	 */
	public static GWDirection getDirection(GWCoordinate startCoord, GWCoordinate endCoord) {
		if (!isAdjacent(startCoord, endCoord))
			return null;
		
		// assume movement only in the four cardinal directions
		if (endCoord.getX() < startCoord.getX())
			return GWDirection.WEST;
		else if (endCoord.getX() > startCoord.getX())
			return GWDirection.EAST;
		else if (endCoord.getY() < startCoord.getY())
			return GWDirection.SOUTH;
		else if (endCoord.getY() > startCoord.getY())
			return GWDirection.NORTH;
		else
			return null;
	}
	
	public String toString() {
		return "("+x+","+y+")";
	}
	
	public String toXML() {
		String str = "<coordinates>";
		str += "<x>"+x+"</x>";
		str += "<y>"+y+"</y>";
		str += "</coordinates>";
		return str;
	}

}
